using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;

namespace ContraMetal
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Player player;
        Song songtrack;
        Map map;
        Camera camera;

        ArrayList enemies;

        List<Enemy_fly> enemies_fly = new List<Enemy_fly>(); //eduardo
        Random random = new Random();                       //eduardo
        List<Bullet> bullets = new List<Bullet>();         //eduardo

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            map = new Map();
            player = new Player();
            enemies = new ArrayList();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            camera = new Camera(GraphicsDevice.Viewport);
            map.Load(Content);
            player.Load(Content);
            songtrack = Content.Load<Song>("Songs/Kalimba");
            //MediaPlayer.Play(songtrack);

            //Creamos los enemigos y los metemos en el arraylist
            int enemiesNumber = 10;
            float X = 250;
            float Y = 380;

            for (int i = 0; i < enemiesNumber; i++)
            {
                Vector2 pos = new Vector2(X, Y);
                Enemy e = new Enemy(pos);
                enemies.Add(e);
                X += 490;
            }

            //cargamos las texturas del enemigo
            foreach (Enemy e in enemies)
            {
                e.Load(Content);
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>

        float spawn = 0; //eduardo
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            player.Update(gameTime);
            camera.Update(player.Position, map.RectangleBG.Width, map.RectangleBG.Height);

            foreach (Enemy e in enemies)
            {
                e.Update();
            }

            spawn += (float)gameTime.ElapsedGameTime.TotalSeconds; //eduardo
            foreach (Enemy_fly enemy in enemies_fly)  //eduardo
                enemy.Update(graphics.GraphicsDevice);//eduardo

            LoadEnemies_fly();//eduardo

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
                Shoot();
            UpdateBullets();

            base.Update(gameTime);
        }


        public void LoadEnemies_fly()  //eduardo
        {
            int randY = random.Next(100, 400);
            if (spawn >= 1)
            {
                spawn = 0;
                if (enemies_fly.Count() < 4)
                    enemies_fly.Add(new Enemy_fly(Content.Load<Texture2D>("images/heli"), new Vector2(1100, randY)));
            }
            for (int i = 0; i < enemies_fly.Count; i++)
            {
                if (!enemies_fly[i].isVisible)
                {
                    enemies_fly.RemoveAt(i);
                    i--;
                }
            }
        }                       //eduardo

        public void UpdateBullets()         //eduardo
        {
            foreach (Bullet bullet in bullets)
            {
                bullet.position += bullet.velocity;
                if (Vector2.Distance(bullet.position, player.Position) > 500)
                    bullet.isVisible = false;
            }
            for (int i = 0; i < bullets.Count; i++)
            {
                if (!bullets[i].isVisible)
                {
                    bullets.RemoveAt(i);
                    i--;
                }
            }
        }                                   //eduardo


        public void Shoot()                 //eduardo
        {
            Bullet newBullet = new Bullet(Content.Load<Texture2D>("images/bullet_bill"));
            newBullet.velocity = new Vector2(5, 0);
            newBullet.position = player.Position + newBullet.velocity;
            newBullet.isVisible = true;
            if (bullets.Count() < 20)
                bullets.Add(newBullet);


        }                                   //eduardo
        
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
            map.Draw(spriteBatch);
            player.Draw(spriteBatch);

            foreach (Enemy e in enemies)
            {
                e.Draw(spriteBatch);
            }

            foreach (Enemy_fly enemy_fly in enemies_fly) //eduardo
                enemy_fly.Draw(spriteBatch);                //eduardo
            foreach (Bullet bullet in bullets)          //eduardo
                bullet.Draw(spriteBatch);           //eduardo

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
